package com.uzai.console.service.fd.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.fd.fddeviceplan.FdDevicePlanDto;
import com.uzai.console.dto.fd.fddeviceplan.FdDevicePlanIdDto;
import com.uzai.console.dto.fd.fddeviceplan.FdDevicePlanQuery;
import com.uzai.console.dto.fd.fddeviceplangroup.FdDevicePlanGroupDto;
import com.uzai.console.dto.fd.fdfollowgroupconfig.FdFollowGroupConfigQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.fd.FdDevicePlanService;
import com.uzai.console.vo.fd.fddeviceplan.FdDevicePlanVo;
import com.uzai.console.vo.fd.fddeviceplangroup.FdDevicePlanGroupVo;
import com.uzai.console.vo.fd.fddeviceplangroup.GroupSendDeviceInfoVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 发单设备方案
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class FdDevicePlanServiceImpl implements FdDevicePlanService {

    Logger logger = LoggerFactory.getLogger(FdDevicePlanServiceImpl.class);

    @Autowired
    private FdDevicePlanMapper fdDevicePlanMapper;
    @Autowired
    private FdDevicePlanGroupMapper fdDevicePlanGroupMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private TbTokenMapper tbTokenMapper;
    @Autowired
    private FdDevicePlanGroupTuoMapper fdDevicePlanGroupTuoMapper;
    @Autowired
    private FdFollowGroupConfigMapper fdFollowGroupConfigMapper;

    /**
     * 查询发单设备方案列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanQuery
     * @return null
     */
    public Page<FdDevicePlanVo> findFdDevicePlanList(FdDevicePlanQuery fdDevicePlanQuery){
        //新建返回到页面的LIST对象
        List<FdDevicePlanVo> fdDevicePlanPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<FdDevicePlan> page = new Page<>();
        fdDevicePlanQuery.convert(page);
        ArrayList<FdDevicePlan> fdDevicePlanList =  fdDevicePlanMapper.findByList(page, fdDevicePlanQuery);
        if(fdDevicePlanList != null && fdDevicePlanList.size() > 0){
            for (FdDevicePlan fdDevicePlan : fdDevicePlanList){
                FdDevicePlanVo fdDevicePlanVo = new FdDevicePlanVo();
                BeanUtils.copyProperties(fdDevicePlan, fdDevicePlanVo);
                fdDevicePlanPageVoList.add(fdDevicePlanVo);

                //获取群信息
                List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
                if(fdDevicePlanGroupList != null && fdDevicePlanGroupList.size() > 0){
                    //封装群信息列表
                    List<FdDevicePlanGroupVo> fdDevicePlanGroupVoList = new ArrayList<>();
                    fdDevicePlanVo.setFdDevicePlanGroupVoList(fdDevicePlanGroupVoList);
                    //封装群设备类型
                    Set<String> deviceTypeList = new HashSet<>();
                    fdDevicePlanVo.setDeviceTypeList(deviceTypeList);

                    for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
                        FdDevicePlanGroupVo fdDevicePlanGroupVo = new FdDevicePlanGroupVo();
                        BeanUtils.copyProperties(fdDevicePlanGroup, fdDevicePlanGroupVo);
                        //封装群信息
                        fdDevicePlanGroupVoList.add(fdDevicePlanGroupVo);
                        //封装群设备类型
                        deviceTypeList.add(fdDevicePlanGroup.getDeviceType());
                        //发送消息机器人信息
                        if(fdDevicePlanGroup.getGroupSendDeviceUniqueId() != null){
                            Device device = deviceService.findDeviceInfoByDeviceUniqueId(fdDevicePlanGroup.getGroupSendDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                            if(device != null){
                                GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                                BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                                fdDevicePlanGroupVo.setGroupSendDeviceInfoVo(groupSendDeviceInfoVo);
                            }
                        }

                        //托机器人信息列表
                        if(StringUtils.isNotBlank(fdDevicePlanGroup.getTuoDeviceUniqueIds())){
                            try {
                                List<Long> tuoDeviceUniqueIdList = JSONObject.parseArray(fdDevicePlanGroup.getTuoDeviceUniqueIds(), Long.class);
                                if(tuoDeviceUniqueIdList != null && tuoDeviceUniqueIdList.size() > 0){
                                    List<FdDevicePlanGroupVo.TuoDeviceInfo> tuoDeviceInfoList = new ArrayList<>();
                                    for (Long tuoDeviceUniqueId : tuoDeviceUniqueIdList) {
                                        FdDevicePlanGroupVo.TuoDeviceInfo tuoDeviceInfo = new FdDevicePlanGroupVo.TuoDeviceInfo();
                                        tuoDeviceInfo.setDeviceUniqueId(tuoDeviceUniqueId);
                                        //托机器人信息
                                        Device device = deviceService.findDeviceInfoByDeviceUniqueId(tuoDeviceInfo.getDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                                        if(device != null){
                                            GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                                            BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                                            tuoDeviceInfo.setTuoDeviceInfoVo(groupSendDeviceInfoVo);
                                        }
                                        tuoDeviceInfoList.add(tuoDeviceInfo);
                                    }
                                    fdDevicePlanGroupVo.setTuoDeviceInfoList(tuoDeviceInfoList);
                                }
                            }catch (Exception e ){
                            }
                        }
                    }
                }

                //气氛组常用语设置
                String tuoComContents = fdDevicePlan.getTuoComContents();
                if(StringUtils.isNotBlank(tuoComContents)){
                    try {
                        List<FdDevicePlanVo.TuoComContent> tuoComContentList = JSONObject.parseArray(tuoComContents, FdDevicePlanVo.TuoComContent.class);
                        fdDevicePlanVo.setTuoComContentList(tuoComContentList);
                    }catch (Exception e){
                        
                    }
                }
            }
        }

        //返回到页面分页对象
        Page<FdDevicePlanVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(fdDevicePlanPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 分页查询发单设备方案列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanQuery
     * @return null
     */
    public Page<FdDevicePlanVo> findFdDevicePlanByPage(FdDevicePlanQuery fdDevicePlanQuery){
        //新建返回到页面的LIST对象
        List<FdDevicePlanVo> fdDevicePlanPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<FdDevicePlan> page = new Page<>();
        fdDevicePlanQuery.convert(page);
        ArrayList<FdDevicePlan> fdDevicePlanList =  fdDevicePlanMapper.findByList(page, fdDevicePlanQuery);
        if(fdDevicePlanList != null && fdDevicePlanList.size() > 0){
            for (FdDevicePlan fdDevicePlan : fdDevicePlanList){
                FdDevicePlanVo fdDevicePlanVo = new FdDevicePlanVo();
                BeanUtils.copyProperties(fdDevicePlan, fdDevicePlanVo);
                fdDevicePlanPageVoList.add(fdDevicePlanVo);

                //获取群信息
                List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
                if(fdDevicePlanGroupList != null && fdDevicePlanGroupList.size() > 0){
                    //封装群信息列表
                    List<FdDevicePlanGroupVo> fdDevicePlanGroupVoList = new ArrayList<>();
                    fdDevicePlanVo.setFdDevicePlanGroupVoList(fdDevicePlanGroupVoList);
                    //封装群设备类型
                    Set<String> deviceTypeList = new HashSet<>();
                    fdDevicePlanVo.setDeviceTypeList(deviceTypeList);

                    for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
                        FdDevicePlanGroupVo fdDevicePlanGroupVo = new FdDevicePlanGroupVo();
                        BeanUtils.copyProperties(fdDevicePlanGroup, fdDevicePlanGroupVo);
                        //封装群信息
                        fdDevicePlanGroupVoList.add(fdDevicePlanGroupVo);
                        //封装群设备类型
                        deviceTypeList.add(fdDevicePlanGroup.getDeviceType());
                    }
                }
            }
        }

        //返回到页面分页对象
        Page<FdDevicePlanVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(fdDevicePlanPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }


    /**
     * 查询发单设备方案详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanIdDto
     * @return null
     */
    public FdDevicePlanVo findFdDevicePlan(FdDevicePlanIdDto fdDevicePlanIdDto){
        if(fdDevicePlanIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        FdDevicePlan fdDevicePlan = fdDevicePlanMapper.selectById(fdDevicePlanIdDto.getId(), fdDevicePlanIdDto.getMerId());

        if(fdDevicePlan == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该发单设备方案不存在");
        }

        FdDevicePlanVo fdDevicePlanVo = new FdDevicePlanVo();
        BeanUtils.copyProperties(fdDevicePlan, fdDevicePlanVo);

        //封装群内个人设置淘口令前缀列表
        if(Strings.isNotBlank(fdDevicePlan.getPwdpres())){
            try {
                List<String> stringList = JSONObject.parseArray(fdDevicePlan.getPwdpres(), String.class);
                fdDevicePlanVo.setPwdpreList(stringList);
            }catch (Exception e){

            }
        }

        //封装群内个人设置淘口令后缀
        if(Strings.isNotBlank(fdDevicePlan.getPwdsufs())){
            try {
                List<String> stringList = JSONObject.parseArray(fdDevicePlan.getPwdsufs(), String.class);
                fdDevicePlanVo.setPwdsufList(stringList);
            }catch (Exception e){

            }
        }

        //获取群信息
        List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
        if(fdDevicePlanGroupList != null && fdDevicePlanGroupList.size() > 0){
            //封装群信息列表
            List<FdDevicePlanGroupVo> fdDevicePlanGroupVoList = new ArrayList<>();
            fdDevicePlanVo.setFdDevicePlanGroupVoList(fdDevicePlanGroupVoList);
            //封装群设备类型
            Set<String> deviceTypeList = new HashSet<>();
            fdDevicePlanVo.setDeviceTypeList(deviceTypeList);
            for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
                FdDevicePlanGroupVo fdDevicePlanGroupVo = new FdDevicePlanGroupVo();
                BeanUtils.copyProperties(fdDevicePlanGroup, fdDevicePlanGroupVo);

                //赋值淘宝联盟对象
                if(fdDevicePlanGroupVo.getTbTokenId() != null ){
                    TbToken tbToken = tbTokenMapper.selectById(fdDevicePlanGroupVo.getTbTokenId(), fdDevicePlanIdDto.getMerId());
                    if(tbToken != null ){
                        TbTokenInfoVo tbTokenInfoVo = new TbTokenInfoVo();
                        BeanUtils.copyProperties(tbToken, tbTokenInfoVo);
                        fdDevicePlanGroupVo.setTbTokenVo(tbTokenInfoVo);
                    }
                }

                //封装群内个人设置淘口令前缀列表
                if(Strings.isNotBlank(fdDevicePlanGroup.getPwdpres())){
                    try {
                        List<String> stringList = JSONObject.parseArray(fdDevicePlanGroup.getPwdpres(), String.class);
                        fdDevicePlanGroupVo.setPwdpreList(stringList);
                    }catch (Exception e){

                    }
                }
                //封装群内个人设置淘口令后缀
                if(Strings.isNotBlank(fdDevicePlanGroup.getPwdsufs())){
                    try {
                        List<String> stringList = JSONObject.parseArray(fdDevicePlanGroup.getPwdsufs(), String.class);
                        fdDevicePlanGroupVo.setPwdsufList(stringList);
                    }catch (Exception e){

                    }
                }

                //封装群信息
                fdDevicePlanGroupVoList.add(fdDevicePlanGroupVo);
                //封装群设备类型
                deviceTypeList.add(fdDevicePlanGroup.getDeviceType());

                //发送消息机器人信息
                if(fdDevicePlanGroup.getGroupSendDeviceUniqueId() != null){
                    Device device = deviceService.findDeviceInfoByDeviceUniqueId(fdDevicePlanGroup.getGroupSendDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                    if(device != null){
                        GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                        BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                        fdDevicePlanGroupVo.setGroupSendDeviceInfoVo(groupSendDeviceInfoVo);
                    }
                }

                //托机器人信息列表
                if(StringUtils.isNotBlank(fdDevicePlanGroup.getTuoDeviceUniqueIds())){
                    try {
                        List<Long> tuoDeviceUniqueIdList = JSONObject.parseArray(fdDevicePlanGroup.getTuoDeviceUniqueIds(), Long.class);
                        if(tuoDeviceUniqueIdList != null && tuoDeviceUniqueIdList.size() > 0){
                            List<FdDevicePlanGroupVo.TuoDeviceInfo> tuoDeviceInfoList = new ArrayList<>();
                            for (Long tuoDeviceUniqueId : tuoDeviceUniqueIdList) {
                                FdDevicePlanGroupVo.TuoDeviceInfo tuoDeviceInfo = new FdDevicePlanGroupVo.TuoDeviceInfo();
                                tuoDeviceInfo.setDeviceUniqueId(tuoDeviceUniqueId);
                                //托机器人信息
                                Device device = deviceService.findDeviceInfoByDeviceUniqueId(tuoDeviceInfo.getDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                                if(device != null){
                                    GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                                    BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                                    tuoDeviceInfo.setTuoDeviceInfoVo(groupSendDeviceInfoVo);
                                }
                                tuoDeviceInfoList.add(tuoDeviceInfo);
                            }
                            fdDevicePlanGroupVo.setTuoDeviceInfoList(tuoDeviceInfoList);
                        }
                    }catch (Exception e ){
                    }
                }
            }
        }

        //气氛组常用语设置
        String tuoComContents = fdDevicePlan.getTuoComContents();
        if(StringUtils.isNotBlank(tuoComContents)){
            try {
                List<FdDevicePlanVo.TuoComContent> tuoComContentList = JSONObject.parseArray(tuoComContents, FdDevicePlanVo.TuoComContent.class);
                fdDevicePlanVo.setTuoComContentList(tuoComContentList);
            }catch (Exception e){

            }
        }
        return fdDevicePlanVo;
    }

    /**
     * 添加发单设备方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanDto
     * @return null
     */
    @Transactional
    public void addFdDevicePlan(FdDevicePlanDto fdDevicePlanDto){

        if(StringUtils.isBlank(fdDevicePlanDto.getName())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入方案名称");
        }

        if(fdDevicePlanDto.getSendRule() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送规则");
        }

        if(fdDevicePlanDto.getGroupSendGap() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入群之间发送间隔时间");
        }

        //群信息
        List<FdDevicePlanGroupDto> fdDevicePlanGroupDtoList = fdDevicePlanDto.getFdDevicePlanGroupDtoList();
        if(fdDevicePlanGroupDtoList == null || fdDevicePlanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加群信息");
        }

        for (FdDevicePlanGroupDto fdDevicePlanGroupDto : fdDevicePlanGroupDtoList) {
            if(fdDevicePlanGroupDto.getGroupSendDeviceUniqueId() == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "群发消息机器人不能为空");
            }

            //选择了气氛组机器人，则要求气氛组设置必须条件
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0){
                if(fdDevicePlanDto.getTuoGteContent() == null || fdDevicePlanDto.getTuoLtContent() == null){
                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择气氛组设置");
                }
            }
        }

        //先添加设备方案
        Integer now = DateUtil.getNowTime();

        FdDevicePlan fdDevicePlan = new FdDevicePlan();
        fdDevicePlan.setId(IdWorker.getId());
        fdDevicePlan.setMerId(fdDevicePlanDto.getMerId());
        fdDevicePlan.setName(fdDevicePlanDto.getName());
        fdDevicePlan.setTokenSet(fdDevicePlanDto.getTokenSet());
        fdDevicePlan.setTbTokenId(fdDevicePlanDto.getTbTokenId());
        fdDevicePlan.setTbPid(fdDevicePlanDto.getTbPid());
        fdDevicePlan.setTbRid(fdDevicePlanDto.getTbRid());
        fdDevicePlan.setPwdpres(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getPwdpreList())));
        fdDevicePlan.setPwdsufs(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getPwdsufList())));
        fdDevicePlan.setMiddlePageType(fdDevicePlanDto.getMiddlePageType());
        fdDevicePlan.setMiddlePageUrl(fdDevicePlanDto.getMiddlePageUrl());
        fdDevicePlan.setJdTokenId(fdDevicePlanDto.getJdTokenId());
        fdDevicePlan.setPddTokenType(fdDevicePlanDto.getPddTokenType());
        fdDevicePlan.setPddTlbTokenId(fdDevicePlanDto.getPddTlbTokenId());
        fdDevicePlan.setPddTokenId(fdDevicePlanDto.getPddTokenId());
        fdDevicePlan.setVphTokenId(fdDevicePlanDto.getVphTokenId());
        fdDevicePlan.setDyTokenType(fdDevicePlanDto.getDyTokenType());
        fdDevicePlan.setDyTlbTokenId(fdDevicePlanDto.getDyTlbTokenId());
        fdDevicePlan.setDyTokenId(fdDevicePlanDto.getDyTokenId());
        fdDevicePlan.setSendRule(fdDevicePlanDto.getSendRule());
        fdDevicePlan.setGroupSendGap(fdDevicePlanDto.getGroupSendGap());
        fdDevicePlan.setTuoGteContent(fdDevicePlanDto.getTuoGteContent());
        fdDevicePlan.setTuoLtContent(fdDevicePlanDto.getTuoLtContent());
        fdDevicePlan.setTuoComContents(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getTuoComContentList())));
        fdDevicePlan.setCreateTime(now);
        fdDevicePlan.setUpdateTime(now);
        fdDevicePlanMapper.insertSelective(fdDevicePlan);

        //保存群发设备方案群信息
        for (FdDevicePlanGroupDto fdDevicePlanGroupDto : fdDevicePlanGroupDtoList) {
            FdDevicePlanGroup fdDevicePlanGroup = new FdDevicePlanGroup();
            fdDevicePlanGroup.setId(IdWorker.getId());
            fdDevicePlanGroup.setMerId(fdDevicePlan.getMerId());
            fdDevicePlanGroup.setFdDevicePlanId(fdDevicePlan.getId());
            fdDevicePlanGroup.setDeviceType(fdDevicePlanGroupDto.getDeviceType());
            fdDevicePlanGroup.setGroupId(fdDevicePlanGroupDto.getGroupId());
            fdDevicePlanGroup.setNick(fdDevicePlanGroupDto.getNick());
            fdDevicePlanGroup.setDeviceUniqueId(fdDevicePlanGroupDto.getDeviceUniqueId());
            fdDevicePlanGroup.setDeviceId(fdDevicePlanGroupDto.getDeviceId());
            fdDevicePlanGroup.setWxid(fdDevicePlanGroupDto.getWxid());
            fdDevicePlanGroup.setGroupSendDeviceUniqueId(fdDevicePlanGroupDto.getGroupSendDeviceUniqueId());
            //托机器人列表
            List<Long> tuoDeviceUniqueIdList = new ArrayList<>();
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0){
                for (FdDevicePlanGroupDto.TuoDeviceInfo tuoDeviceInfo : fdDevicePlanGroupDto.getTuoDeviceInfoList()){
                    tuoDeviceUniqueIdList.add(tuoDeviceInfo.getDeviceUniqueId());
                }
            }

            fdDevicePlanGroup.setTuoDeviceUniqueIds(Tools.getStrEmpty(JSONObject.toJSONString(tuoDeviceUniqueIdList)));

            fdDevicePlanGroup.setTbTokenId(fdDevicePlanGroupDto.getTbTokenId());
            fdDevicePlanGroup.setTbPid(fdDevicePlanGroupDto.getTbPid());
            fdDevicePlanGroup.setTbRid(fdDevicePlanGroupDto.getTbRid());
            fdDevicePlanGroup.setPwdpres(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanGroupDto.getPwdpreList())));
            fdDevicePlanGroup.setPwdsufs(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanGroupDto.getPwdsufList())));
            fdDevicePlanGroup.setMiddlePageType(fdDevicePlanGroupDto.getMiddlePageType());
            fdDevicePlanGroup.setMiddlePageUrl(fdDevicePlanGroupDto.getMiddlePageUrl());
            fdDevicePlanGroup.setJdTokenId(fdDevicePlanGroupDto.getJdTokenId());
            fdDevicePlanGroup.setPddTokenType(fdDevicePlanGroupDto.getPddTokenType());
            fdDevicePlanGroup.setPddTlbTokenId(fdDevicePlanGroupDto.getPddTlbTokenId());
            fdDevicePlanGroup.setPddTokenId(fdDevicePlanGroupDto.getPddTokenId());
            fdDevicePlanGroup.setVphTokenId(fdDevicePlanGroupDto.getVphTokenId());
            fdDevicePlanGroup.setDyTokenType(fdDevicePlanGroupDto.getDyTokenType());
            fdDevicePlanGroup.setDyTlbTokenId(fdDevicePlanGroupDto.getDyTlbTokenId());
            fdDevicePlanGroup.setDyTokenId(fdDevicePlanGroupDto.getDyTokenId());
            fdDevicePlanGroup.setCreateTime(now);
            fdDevicePlanGroup.setUpdateTime(now);
            fdDevicePlanGroupMapper.insertSelective(fdDevicePlanGroup);

            //保存托机器人信息
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0){
                for (FdDevicePlanGroupDto.TuoDeviceInfo tuoDeviceInfo : fdDevicePlanGroupDto.getTuoDeviceInfoList()) {
                    FdDevicePlanGroupTuo fdDevicePlanGroupTuo = new FdDevicePlanGroupTuo();
                    fdDevicePlanGroupTuo.setId(IdWorker.getId());
                    fdDevicePlanGroupTuo.setMerId(fdDevicePlan.getMerId());
                    fdDevicePlanGroupTuo.setFdDevicePlanId(fdDevicePlan.getId());
                    fdDevicePlanGroupTuo.setFdDevicePlanGroupId(fdDevicePlanGroup.getId());
                    fdDevicePlanGroupTuo.setGroupId(fdDevicePlanGroup.getGroupId());
                    fdDevicePlanGroupTuo.setTuoDeviceUniqueId(tuoDeviceInfo.getDeviceUniqueId());
                    fdDevicePlanGroupTuo.setSendTime(0); //未发送默认为0
                    fdDevicePlanGroupTuo.setCreateTime(now);
                    fdDevicePlanGroupTuo.setUpdateTime(now);
                    fdDevicePlanGroupTuoMapper.insertSelective(fdDevicePlanGroupTuo);
                }
            }
        }
    }

    /**
     * 修改发单设备方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanDto
     * @return null
     */
    @Transactional
    public void updateFdDevicePlan(FdDevicePlanDto fdDevicePlanDto){

        if(fdDevicePlanDto.getId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入方案id");
        }

        if(StringUtils.isBlank(fdDevicePlanDto.getName())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入方案名称");
        }

        if(fdDevicePlanDto.getSendRule() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送规则");
        }

        if(fdDevicePlanDto.getGroupSendGap() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入群之间发送间隔时间");
        }

        //群信息
        List<FdDevicePlanGroupDto> fdDevicePlanGroupDtoList = fdDevicePlanDto.getFdDevicePlanGroupDtoList();
        if(fdDevicePlanGroupDtoList == null || fdDevicePlanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加群信息");
        }

        for (FdDevicePlanGroupDto fdDevicePlanGroupDto : fdDevicePlanGroupDtoList) {
            if(fdDevicePlanGroupDto.getGroupSendDeviceUniqueId() == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "群发消息机器人不能为空");
            }

            //选择了气氛组机器人，则要求气氛组设置必须条件
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0){
                if(fdDevicePlanDto.getTuoGteContent() == null || fdDevicePlanDto.getTuoLtContent() == null){
                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择气氛组设置");
                }
            }
        }

        FdDevicePlan fdDevicePlan = fdDevicePlanMapper.selectById(fdDevicePlanDto.getId(), fdDevicePlanDto.getMerId());
        if(fdDevicePlan == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该设备方案不存在");
        }

        //判断是否有自动跟发采集群配置任务使用了该设备方案，如果有，则先关闭才能修改
        FdFollowGroupConfigQuery fdFollowGroupConfigQuery = new FdFollowGroupConfigQuery();
        fdFollowGroupConfigQuery.setMerId(fdDevicePlanDto.getMerId());
        fdFollowGroupConfigQuery.setStatus(1);//启用的采集群配置任务
        fdFollowGroupConfigQuery.setFdDevicePlanId(fdDevicePlanDto.getId());
        List<FdFollowGroupConfig> fdFollowGroupConfigList = fdFollowGroupConfigMapper.findByList(fdFollowGroupConfigQuery);
        if(fdFollowGroupConfigList != null && fdFollowGroupConfigList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该设备方案正在被采集群自动发任务启用，请先关闭采集群任务");
        }

        Integer now = DateUtil.getNowTime();
        //修改发单设备方案
        fdDevicePlan.setName(fdDevicePlanDto.getName());
        fdDevicePlan.setTokenSet(fdDevicePlanDto.getTokenSet());
        fdDevicePlan.setTbTokenId(fdDevicePlanDto.getTbTokenId());
        fdDevicePlan.setTbPid(fdDevicePlanDto.getTbPid());
        fdDevicePlan.setTbRid(fdDevicePlanDto.getTbRid());
        fdDevicePlan.setPwdpres(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getPwdpreList())));
        fdDevicePlan.setPwdsufs(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getPwdsufList())));
        fdDevicePlan.setMiddlePageType(fdDevicePlanDto.getMiddlePageType());
        fdDevicePlan.setMiddlePageUrl(fdDevicePlanDto.getMiddlePageUrl());
        fdDevicePlan.setJdTokenId(fdDevicePlanDto.getJdTokenId());
        fdDevicePlan.setPddTokenType(fdDevicePlanDto.getPddTokenType());
        fdDevicePlan.setPddTlbTokenId(fdDevicePlanDto.getPddTlbTokenId());
        fdDevicePlan.setPddTokenId(fdDevicePlanDto.getPddTokenId());
        fdDevicePlan.setVphTokenId(fdDevicePlanDto.getVphTokenId());
        fdDevicePlan.setDyTokenType(fdDevicePlanDto.getDyTokenType());
        fdDevicePlan.setDyTlbTokenId(fdDevicePlanDto.getDyTlbTokenId());
        fdDevicePlan.setDyTokenId(fdDevicePlanDto.getDyTokenId());
        fdDevicePlan.setSendRule(fdDevicePlanDto.getSendRule());
        fdDevicePlan.setGroupSendGap(fdDevicePlanDto.getGroupSendGap());
        fdDevicePlan.setTuoGteContent(fdDevicePlanDto.getTuoGteContent());
        fdDevicePlan.setTuoLtContent(fdDevicePlanDto.getTuoLtContent());
        fdDevicePlan.setTuoComContents(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanDto.getTuoComContentList())));
        fdDevicePlan.setUpdateTime(now);
        fdDevicePlanMapper.updateByIdSelective(fdDevicePlan);

        //修改发单群信息
        //批量删除该发单群信息
        fdDevicePlanGroupMapper.deleteByByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
        //批量删除该发单群托机器人信息
        fdDevicePlanGroupTuoMapper.deleteByByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
        //保存群发设备方案群信息
        for (FdDevicePlanGroupDto fdDevicePlanGroupDto : fdDevicePlanGroupDtoList) {
            FdDevicePlanGroup fdDevicePlanGroup = new FdDevicePlanGroup();
            fdDevicePlanGroup.setId(IdWorker.getId());
            fdDevicePlanGroup.setMerId(fdDevicePlan.getMerId());
            fdDevicePlanGroup.setFdDevicePlanId(fdDevicePlan.getId());
            fdDevicePlanGroup.setDeviceType(fdDevicePlanGroupDto.getDeviceType());
            fdDevicePlanGroup.setGroupId(fdDevicePlanGroupDto.getGroupId());
            fdDevicePlanGroup.setNick(fdDevicePlanGroupDto.getNick());
            fdDevicePlanGroup.setDeviceUniqueId(fdDevicePlanGroupDto.getDeviceUniqueId());
            fdDevicePlanGroup.setDeviceId(fdDevicePlanGroupDto.getDeviceId());
            fdDevicePlanGroup.setWxid(fdDevicePlanGroupDto.getWxid());
            fdDevicePlanGroup.setGroupSendDeviceUniqueId(fdDevicePlanGroupDto.getGroupSendDeviceUniqueId());

            //托机器人列表
            List<Long> tuoDeviceUniqueIdList = new ArrayList<>();
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0){
                for (FdDevicePlanGroupDto.TuoDeviceInfo tuoDeviceInfo : fdDevicePlanGroupDto.getTuoDeviceInfoList()){
                    tuoDeviceUniqueIdList.add(tuoDeviceInfo.getDeviceUniqueId());
                }

            }
            fdDevicePlanGroup.setTuoDeviceUniqueIds(Tools.getStrEmpty(JSONObject.toJSONString(tuoDeviceUniqueIdList)));
            fdDevicePlanGroup.setTbTokenId(fdDevicePlanGroupDto.getTbTokenId());
            fdDevicePlanGroup.setTbPid(fdDevicePlanGroupDto.getTbPid());
            fdDevicePlanGroup.setTbRid(fdDevicePlanGroupDto.getTbRid());
            fdDevicePlanGroup.setPwdpres(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanGroupDto.getPwdpreList())));
            fdDevicePlanGroup.setPwdsufs(Tools.getStrEmpty(JSONObject.toJSONString(fdDevicePlanGroupDto.getPwdsufList())));
            fdDevicePlanGroup.setMiddlePageType(fdDevicePlanGroupDto.getMiddlePageType());
            fdDevicePlanGroup.setMiddlePageUrl(fdDevicePlanGroupDto.getMiddlePageUrl());
            fdDevicePlanGroup.setJdTokenId(fdDevicePlanGroupDto.getJdTokenId());
            fdDevicePlanGroup.setPddTokenType(fdDevicePlanGroupDto.getPddTokenType());
            fdDevicePlanGroup.setPddTlbTokenId(fdDevicePlanGroupDto.getPddTlbTokenId());
            fdDevicePlanGroup.setPddTokenId(fdDevicePlanGroupDto.getPddTokenId());
            fdDevicePlanGroup.setVphTokenId(fdDevicePlanGroupDto.getVphTokenId());
            fdDevicePlanGroup.setDyTokenType(fdDevicePlanGroupDto.getDyTokenType());
            fdDevicePlanGroup.setDyTlbTokenId(fdDevicePlanGroupDto.getDyTlbTokenId());
            fdDevicePlanGroup.setDyTokenId(fdDevicePlanGroupDto.getDyTokenId());
            fdDevicePlanGroup.setCreateTime(now);
            fdDevicePlanGroup.setUpdateTime(now);
            fdDevicePlanGroupMapper.insertSelective(fdDevicePlanGroup);

            //保存托机器人信息
            if(fdDevicePlanGroupDto.getTuoDeviceInfoList() != null && fdDevicePlanGroupDto.getTuoDeviceInfoList().size() > 0) {
                for (FdDevicePlanGroupDto.TuoDeviceInfo tuoDeviceInfo : fdDevicePlanGroupDto.getTuoDeviceInfoList()) {
                    FdDevicePlanGroupTuo fdDevicePlanGroupTuo = new FdDevicePlanGroupTuo();
                    fdDevicePlanGroupTuo.setId(IdWorker.getId());
                    fdDevicePlanGroupTuo.setMerId(fdDevicePlan.getMerId());
                    fdDevicePlanGroupTuo.setFdDevicePlanId(fdDevicePlan.getId());
                    fdDevicePlanGroupTuo.setFdDevicePlanGroupId(fdDevicePlanGroup.getId());
                    fdDevicePlanGroupTuo.setGroupId(fdDevicePlanGroup.getGroupId());
                    fdDevicePlanGroupTuo.setTuoDeviceUniqueId(tuoDeviceInfo.getDeviceUniqueId());
                    fdDevicePlanGroupTuo.setSendTime(0); //未发送默认为0
                    fdDevicePlanGroupTuo.setCreateTime(now);
                    fdDevicePlanGroupTuo.setUpdateTime(now);
                    fdDevicePlanGroupTuoMapper.insertSelective(fdDevicePlanGroupTuo);
                }
            }
        }
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param fdDevicePlanIdDto
     * @return null
     */
    @Transactional
    public void deleteFdDevicePlan(FdDevicePlanIdDto fdDevicePlanIdDto){
        if(fdDevicePlanIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //判断是否有自动跟发采集群配置任务使用了该设备方案，如果有，则先关闭才能修改
        FdFollowGroupConfigQuery fdFollowGroupConfigQuery = new FdFollowGroupConfigQuery();
        fdFollowGroupConfigQuery.setMerId(fdDevicePlanIdDto.getMerId());
        fdFollowGroupConfigQuery.setStatus(1);//启用的采集群配置任务
        fdFollowGroupConfigQuery.setFdDevicePlanId(fdDevicePlanIdDto.getId());
        List<FdFollowGroupConfig> fdFollowGroupConfigList = fdFollowGroupConfigMapper.findByList(fdFollowGroupConfigQuery);
        if(fdFollowGroupConfigList != null && fdFollowGroupConfigList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该设备方案正在被采集群自动跟发任务启用，请先关闭采集群任务");
        }

        // TODO: 2022/3/8  查询该设备方案是否已经给发单占用了
        //删除设备方案
        fdDevicePlanMapper.deleteById(fdDevicePlanIdDto.getId(), fdDevicePlanIdDto.getMerId());
        //删除设备方案群信息
        fdDevicePlanGroupMapper.deleteByByFdDevicePlanId(fdDevicePlanIdDto.getId(), fdDevicePlanIdDto.getMerId());
        //清除托机器人
        fdDevicePlanGroupTuoMapper.deleteByByFdDevicePlanId(fdDevicePlanIdDto.getId(), fdDevicePlanIdDto.getMerId());
    }

    /**
     * 查询发单设备方案下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<FdDevicePlanVo> findFdDevicePlanListByAll(FdDevicePlanQuery query){
        List<FdDevicePlanVo> fdDevicePlanVoList = new ArrayList<>();
        List<FdDevicePlan> fdDevicePlanList =  fdDevicePlanMapper.findByList(query);
        if(fdDevicePlanList != null && fdDevicePlanList.size() > 0){
            for (FdDevicePlan fdDevicePlan : fdDevicePlanList){
                FdDevicePlanVo fdDevicePlanVo = new FdDevicePlanVo();
                BeanUtils.copyProperties(fdDevicePlan, fdDevicePlanVo);


                //获取群信息
                List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdDevicePlan.getId(), fdDevicePlan.getMerId());
                if(fdDevicePlanGroupList != null && fdDevicePlanGroupList.size() > 0){
                    //封装群信息列表
                    List<FdDevicePlanGroupVo> fdDevicePlanGroupVoList = new ArrayList<>();
                    fdDevicePlanVo.setFdDevicePlanGroupVoList(fdDevicePlanGroupVoList);
                    //封装群设备类型
                    Set<String> deviceTypeList = new HashSet<>();
                    fdDevicePlanVo.setDeviceTypeList(deviceTypeList);

                    for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
                        FdDevicePlanGroupVo fdDevicePlanGroupVo = new FdDevicePlanGroupVo();
                        BeanUtils.copyProperties(fdDevicePlanGroup, fdDevicePlanGroupVo);
                        //封装群信息
                        fdDevicePlanGroupVoList.add(fdDevicePlanGroupVo);
                        //封装群设备类型
                        deviceTypeList.add(fdDevicePlanGroup.getDeviceType());
                        //发送消息机器人信息
                        if(fdDevicePlanGroup.getGroupSendDeviceUniqueId() != null){
                            Device device = deviceService.findDeviceInfoByDeviceUniqueId(fdDevicePlanGroup.getGroupSendDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                            if(device != null){
                                GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                                BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                                fdDevicePlanGroupVo.setGroupSendDeviceInfoVo(groupSendDeviceInfoVo);
                            }
                        }

                        //托机器人信息列表
                        if(StringUtils.isNotBlank(fdDevicePlanGroup.getTuoDeviceUniqueIds())){
                            try {
                                List<Long> tuoDeviceUniqueIdList = JSONObject.parseArray(fdDevicePlanGroup.getTuoDeviceUniqueIds(), Long.class);
                                if(tuoDeviceUniqueIdList != null && tuoDeviceUniqueIdList.size() > 0){
                                    List<FdDevicePlanGroupVo.TuoDeviceInfo> tuoDeviceInfoList = new ArrayList<>();
                                    for (Long tuoDeviceUniqueId : tuoDeviceUniqueIdList) {
                                        FdDevicePlanGroupVo.TuoDeviceInfo tuoDeviceInfo = new FdDevicePlanGroupVo.TuoDeviceInfo();
                                        tuoDeviceInfo.setDeviceUniqueId(tuoDeviceUniqueId);
                                        //托机器人信息
                                        Device device = deviceService.findDeviceInfoByDeviceUniqueId(tuoDeviceInfo.getDeviceUniqueId(), fdDevicePlanGroup.getDeviceType(), fdDevicePlanGroup.getMerId());
                                        if(device != null){
                                            GroupSendDeviceInfoVo groupSendDeviceInfoVo = new GroupSendDeviceInfoVo();
                                            BeanUtils.copyProperties(device, groupSendDeviceInfoVo);
                                            tuoDeviceInfo.setTuoDeviceInfoVo(groupSendDeviceInfoVo);
                                        }
                                        tuoDeviceInfoList.add(tuoDeviceInfo);
                                    }
                                    fdDevicePlanGroupVo.setTuoDeviceInfoList(tuoDeviceInfoList);
                                }
                            }catch (Exception e ){
                            }
                        }
                    }
                }

                //气氛组常用语设置
                String tuoComContents = fdDevicePlan.getTuoComContents();
                if(StringUtils.isNotBlank(tuoComContents)){
                    try {
                        List<FdDevicePlanVo.TuoComContent> tuoComContentList = JSONObject.parseArray(tuoComContents, FdDevicePlanVo.TuoComContent.class);
                        fdDevicePlanVo.setTuoComContentList(tuoComContentList);
                    }catch (Exception e){

                    }
                }

                fdDevicePlanVoList.add(fdDevicePlanVo);
            }
        }
        return fdDevicePlanVoList;
    }


    /**
     * 查询所有发单设备方案列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<FdDevicePlanVo> findFdDevicePlanListSimple(FdDevicePlanQuery query){
        List<FdDevicePlanVo> fdDevicePlanVoList = new ArrayList<>();
        List<FdDevicePlan> fdDevicePlanList =  fdDevicePlanMapper.findByList(query);
        if(fdDevicePlanList != null && fdDevicePlanList.size() > 0){
            for (FdDevicePlan fdDevicePlan : fdDevicePlanList){
                FdDevicePlanVo fdDevicePlanVo = new FdDevicePlanVo();
                BeanUtils.copyProperties(fdDevicePlan, fdDevicePlanVo);
                fdDevicePlanVoList.add(fdDevicePlanVo);
            }
        }
        return fdDevicePlanVoList;
    }
}
